package com.animee.app4.slice;

import com.animee.app4.ResourceTable;
import com.animee.app4.bean.NewsBean;
import com.animee.app4.bean.NewsListProvider;
import com.animee.app4.utils.DBManager;
import com.animee.app4.utils.HttpUtils;
import com.google.gson.Gson;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.ListContainer;
import ohos.data.rdb.RdbStore;
import ohos.data.resultset.ResultSet;

import java.security.MessageDigest;
import java.util.ArrayList;
import java.util.List;

public class NewsListAbilitySlice extends AbilitySlice {

    ListContainer listContainer;
    List<NewsBean.ResultDTO.DataDTO> dataList = new ArrayList<>();
    private NewsListProvider provider;

    String url = "http://v.juhe.cn/toutiao/index?key=0af60a86bfa3575d53c1491d1be310ca&type=yule";
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_newslist);
        initView();

        loadData();
    }

    private void loadData() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                String json = HttpUtils.getJsonByNetwork(url);
                if (json!=null && !json.equals("")){
                    NewsBean bean = new Gson().fromJson(json, NewsBean.class);
                    if (bean.getError_code()==0) {
                        List<NewsBean.ResultDTO.DataDTO> list = bean.getResult().getData();
                        dataList.addAll(list);
                        saveNewsToDB(list);
                    }else{
                        List<NewsBean.ResultDTO.DataDTO> list = queryNewsFromDB();
                        dataList.addAll(list);
                    }
                }else{
                    List<NewsBean.ResultDTO.DataDTO> list = queryNewsFromDB();
                    dataList.addAll(list);
                }

                getMainTaskDispatcher().asyncDispatch(new Runnable() {
                    @Override
                    public void run() {
                        provider.notifyDataChanged();
                    }
                });

            }
        }).start();

    }
    private List<NewsBean.ResultDTO.DataDTO> queryNewsFromDB() {
        List<NewsBean.ResultDTO.DataDTO>list = new ArrayList<>();
        RdbStore rdbStore = DBManager.getRdbStore(this);
        String sql = "select * from newtb";
        ResultSet rs = rdbStore.querySql(sql, null);
        while (rs.goToNextRow()) {
            String key = rs.getString(0);
            String title = rs.getString(1);
            String time = rs.getString(2);
            String category = rs.getString(3);
            String author = rs.getString(4);
            String url = rs.getString(5);
            String imgurl1 = rs.getString(6);
            String imgurl2 = rs.getString(7);
            String imgurl3 = rs.getString(8);
            String iscnt = rs.getString(9);
            NewsBean.ResultDTO.DataDTO dto = new NewsBean.ResultDTO.DataDTO(key, title, time, category, author,
                    url, imgurl1, imgurl2, imgurl3, iscnt);
            list.add(dto);
        }
        return list;
    }
    private void saveNewsToDB(List<NewsBean.ResultDTO.DataDTO> list) {
        RdbStore rdbStore = DBManager.getRdbStore(this);
        String sql = "insert into newtb values (?,?,?,?,?,?,?,?,?,?)";
        for (int i = 0; i < list.size(); i++) {
            NewsBean.ResultDTO.DataDTO dto = list.get(i);
            Object []arr = {dto.getUniquekey(),dto.getTitle(),dto.getDate(),dto.getCategory(),dto.getAuthor_name(),
                dto.getUrl(),dto.getThumbnail_pic_s(),dto.getThumbnail_pic_s02(),dto.getThumbnail_pic_s03(),dto.getIs_content()};
            rdbStore.executeSql(sql,arr);
        }
    }
    private void initView() {
        listContainer = findComponentById(ResourceTable.Id_news_lc);
        provider = new NewsListProvider(this, dataList);
        listContainer.setItemProvider(provider);

        listContainer.setItemClickedListener(new ListContainer.ItemClickedListener() {
            @Override
            public void onItemClicked(ListContainer listContainer, Component component, int i, long l) {
                NewsBean.ResultDTO.DataDTO dto = dataList.get(i);
                String path = dto.getUrl();
                Intent intent = new Intent();
                intent.setParam("url",path);
                present(new NewsInfoAbilitySlice(),intent);
            }
        });
    }
}
